var locationData = require('../../../staticData/locationKeyValue.json');
exports.customerDetail = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var c_id = restfulParams.customerId;

    var u_id = 0;

    var c_openid = 0;

    var mediaId = 0;

    //用户基础信息查询
    function customerBasicInformationQuery(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_is_delete/0/c_id/" + c_id,
                method: 'POST',
            },
            function (data) {

                if (data.result.data.length > 0) {
                    resultData.customerBasicInformation = data.result.data;

                    u_id = data.result.data[0].c_uid;

                    var provinceId = data.result.data[0].c_provinces;
                    var cityId = data.result.data[0].c_city;
                    var areaId = data.result.data[0].c_area;
                    resultData.customerBasicInformation[0].provinceName = locationData[provinceId];
                    resultData.customerBasicInformation[0].cityName = locationData[cityId];
                    resultData.customerBasicInformation[0].areaName = locationData[areaId];

                    c_openid = data.result.data[0].c_openid;
                } else {
                    resultData.customerBasicInformation = data.result.data;
                }


                cb();

            }
        )
    }

    //查询跟进记录
    function followUpRecordInquiry(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerFollowRecordSearch.Request_Api_Url + "/count/1/cfr_is_delete/0/cfr_cid/" + c_id + "/sort_order/cfr_id-desc/skip/0/limit/1",
                method: 'POST',
                data: {
                    fields: ["cfr_uid", "cfr_content", "cfr_way", "cfr_create_time", "cfr_next_time", "cfr_comment", "cfr_comment_user", "cfr_name"].join(",")
                }
            },
            function (data) {

                resultData.followUpRecord = data.result.data;
                resultData.followUpRecordCount = data.result.count;

                cb();

            }
        )
    }

    //查询销售信息
    function salesmanData(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_is_delete/0/u_id/" + u_id,
                method: 'POST',
                data: {
                    fields: ['u_id', 'u_nickname','u_is_manager'].join(",")
                }
            },
            function (data) {

                var salesmanData = [];

                // if(data.result.data[0].u_is_manager == 0){
                    salesmanData.push(data.result.data[0])
                // }
                resultData.salesmanData = salesmanData;

                cb();

            }
        )
    }

    //查询客户成单情况
    function customerOrders(cb) {

        if (c_openid != null && c_openid != "") {
            R.SEND_HTTP(
                req,
                res,
                {
                    url: config.external_server.apiAddress + "/organization/search_art_product_order/count/1/apo_status/1/apo_openid/" + c_openid + "/sort_order/apo_id-desc",
                    method: 'POST',
                    data: {
                        fields: ['apo_id', 'apo_name', 'apo_id', 'apo_old_price', 'apo_new_price', 'apo_real_pay', 'apo_create_time'].join(",")
                    }
                },
                function (data) {
                    console.log(data);

                    var getLastOrder = [];

                    var orderTotalFee = 0;

                    if (data.result.data.length > 0) {

                        getLastOrder.push(data.result.data[0]);

                        resultData.customerOrdersCount = data.result.count;

                        for (var i = 0; i < data.result.data.length; i++) {

                            orderTotalFee += parseFloat(data.result.data[i].apo_real_pay)

                        }
                    }
                    resultData.customerOrders = getLastOrder;

                    resultData.orderTotalFee = orderTotalFee.toFixed(2);


                    cb();

                }
            )
        } else {
            resultData.customerOrders = [];
            resultData.customerOrdersCount = 0;
            cb()
        }
    }

    //查询客户最后浏览记录
    function customerFinalBrowseRecord(cb) {

        if (c_openid != null && c_openid != "") {
            R.SEND_HTTP(
                req,
                res,
                {
                    url: config.external_server.apiAddress + "/organization/search_art_product_user_read_record/apurr_openid/" + c_openid + "/sort_order/apurr_id-desc/skip/0/limit/1",
                    method: 'POST',
                    data: {
                        fields: ['apurr_id', 'apurr_create_time', 'apurr_read_count', 'apuur_mcid'].join(",")
                    }
                },
                function (data) {
                    console.log(data);

                    resultData.customerFinalBrowseRecord = data.result.data;

                    if (data.result.data.length > 0) {
                        mediaId = data.result.data[0].apuur_mcid;
                    }

                    cb();

                }
            )
        } else {
            resultData.customerFinalBrowseRecord = [];
            cb()
        }
    }

    //浏览内容
    function browseContent(cb) {

        if (mediaId > 0) {
            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.artMediaCommunicationSearch.Request_Api_Url + "/mc_is_delete/0/mc_id/" + mediaId,
                    method: 'POST',
                    data: {
                        fields: ['mc_title'].join(",")
                    }
                },
                function (data) {
                    console.log(data);

                    resultData.browseContent = data.result.data;

                    cb();

                }
            )
        } else {
            resultData.browseContent = [];
            cb()
        }
    }

    //查询客户服务记录
    function customerBeServicedRecord(cb){
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerServiceSearch.Request_Api_Url + "/cs_is_delete/0/cs_cid/" + c_id,
                method: 'POST',
                data: {
                    fields: ['cs_id'].join(",")
                }
            },
            function (data) {
                console.log(data);

                resultData.serviceRecord = data.result.data;

                cb();

            }
        )
    }


    F.Serial([
        customerBasicInformationQuery,
        followUpRecordInquiry,
        salesmanData,
        customerOrders,
        customerFinalBrowseRecord,
        browseContent,
        customerBeServicedRecord,
    ], () => {
        RES.successResponse(res, resultData);
    })


}